package com.jasic.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.jasic.R;

public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bDetect = (Button) findViewById(R.id.detect);
        Button bPreview = (Button) findViewById(R.id.preview);
        Button bLayout = (Button) findViewById(R.id.layout);
        Button bCapture = (Button) findViewById(R.id.capture);

        bDetect.setOnClickListener(new DetectOnClickListener());
        bPreview.setOnClickListener(new PreviewOnClickListener());

    }

    /**
     * 1、相机状态检测
     */
    class DetectOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "camera detected status is :" + checkCameraHardware(MainActivity.this)
                   , Toast.LENGTH_LONG).show();

            /**
             * Android 2.3 (API Level 9) and later allows you to check the number of cameras available on a device using the Camera.getNumberOfCameras()
             */
//            DateUtil.sleep(1000);
//            Toast.makeText(MainActivity.this,"本机的摄像头个数为：" + Camera.getNumberOfCameras())
        }

        private boolean checkCameraHardware(Context context) {
            if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
                // this device has a camera
                return true;
            } else {
                // no camera on this device
                return false;
            }
        }
    }

    /**
     * 2、打开相机预览
     */
    private class PreviewOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            Intent intent  = new Intent();
            intent.setClass(MainActivity.this, CameraActivity.class);
//            intent.putExtra("", "");
            Toast.makeText(MainActivity.this, "打开相机预览", Toast.LENGTH_LONG).show();
            startActivity(intent);
        }
    }
}
